package cash_access;

import mware_lib.StubSkeletonCom;


public class Account_Stub extends Account{
	private String remoteName;
	private String remoteHostName;
	int port;
	boolean isStub;
	
	public Account_Stub(String remoteName, Integer port, String remoteHostName) {
		this.remoteName = remoteName;
		this.port = port;
		this.remoteHostName = remoteHostName;
	}
	
	public Account_Stub() {
	}
	
	
	@Override
	public void deposit(double amount) {
		if(amount > 0){
			String resu = null;
			try {
				Object[] params = { amount };
				//contact the skeleton to execute operation
				resu = (String) StubSkeletonCom.methodOfObject(this.getClass().getMethod("deposit", double.class),remoteName, params, remoteHostName, port);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (OverdraftException e) {
				e.printStackTrace();
			}catch( Exception e ){
				e.printStackTrace();
			}
			
			StubSkeletonCom.handleIfException(resu);
		}
	}

	@Override
	public void withdraw(double amount) throws OverdraftException {
		String ex = null;
		try {
			Object[] params  = {amount};
			//contact the skeleton to execute operation
			ex = (String) StubSkeletonCom.methodOfObject(this.getClass().getMethod("withdraw", double.class), remoteName, params, remoteHostName, port);
			
			if(ex != null){
				String[] splitted = ex.split(":");
				if(splitted[1].equals(OverdraftException.class.getName())){
					throw new OverdraftException(splitted[2]);
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (Exception e) {
			if(e instanceof OverdraftException){
				throw new OverdraftException(e.getMessage());
			}else{
				System.err.println("Exception in Skeleton: "+e);	
			}
		}
		StubSkeletonCom.handleIfException(ex);
	}

	@Override
	public double getBalance() {
		double ret = 0;
		String resu = null;
		try {
			//contact the skeleton to execute operation
			Object res = StubSkeletonCom.methodOfObject(this.getClass().getMethod("getBalance", (Class<?>[])null), remoteName, null, remoteHostName, port);
			
			if(res == null){
				ret = Double.NaN;
			}else if(res instanceof String){
				resu = (String) res;

			}else if(res instanceof Double){
				ret = (Double) res;
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (OverdraftException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.err.println("Exception in Skeleton: "+e);	
		}
		StubSkeletonCom.handleIfException(resu);
		return ret;
	}
	

}
